package com.cf.apiInstance.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cf.apiInstance.dao.ApiDataMapper;
import com.cf.apiInstance.domain.ApiData;
import com.cf.apiInstance.domain.ApiDataExample;
import com.cf.apiInstance.domain.ApiDataExample.Criteria;
import com.cf.apiInstance.service.ApiDataService;
import com.cf.apiInstance.vo.ApiDataVo;

/**
 * 测试数据实现类
 * 
 * @author: cf
 * @date: 29 Nov 2018 11:05:58 am
 * @vesion: 1.0
 */
@Service
public class ApiDataServiceImpl implements ApiDataService {

	@Resource
	private ApiDataMapper apiDataMapper;

	@Transactional
	@Override
	public void saveApiData(List<ApiDataVo> submit) {
		submit.forEach(apiDataVo -> {
			ApiData apiData = new ApiData();
			apiData.setMessage(apiDataVo.getMessage());
			apiData.setCreateTime(new Date());
			apiDataMapper.insert(apiData);
		});
	}

	@Override
	public ApiData getApiDataById(Integer id) {
		return apiDataMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<ApiData> getApiData(ApiData queryData) {
		ApiDataExample example = new ApiDataExample();
		Criteria criteria = example.createCriteria();
		if (queryData.getId() != null) {
			criteria.andIdEqualTo(queryData.getId());
		}
		if (StringUtils.isNotEmpty(queryData.getMessage())) {
			criteria.andMessageLike("%" + queryData.getMessage() + "%");
		}
		return apiDataMapper.selectByExample(example);
	}

}
